/*
 * Comrade.java
 *
 * Created on January 4, 2008, 12:07 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Comrade {
    private dbConnection dbcon = new dbConnection();
    private String friendName, characterName, ctype;
    
    /** Creates a new instance of Comrade */
    public Comrade() {
    }
    
    public Comrade(String fname, String cname) {
        setFriendName(fname);
        setCharacterName(cname);
        setType("pending");
    }
    
    public boolean checkExistance (String owner, String friend) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Comrade WHERE characterName = ? AND friendName = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, owner);
            stmt.setString(2, friend);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
              ctype = rs.getString("ctype").trim();
              this.setCharacterName(rs.getString("characterName"));
              this.setFriendName(rs.getString("friendName").trim());
              this.setType(rs.getString("ctype").trim());
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    public void addComrade (Comrade c){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        if (!checkExistance(c.getCharacterName(), c.getFriendName()))
        {
            getDbcon().connect();
            query = "INSERT INTO Comrade (characterName, friendName, ctype) VALUES (?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, c.getCharacterName());
                stmt.setString(2, c.getFriendName());
                stmt.setString(3, c.getType());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    
    public Vector viewCComrades (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Comrade> cList;
            
        cList = new Vector<Comrade>();
        Iterator it = cList.iterator();
                
        getDbcon().connect();
        query = "SELECT characterName, friendName FROM Comrade WHERE characterName = ? AND ctype = ? ORDER BY friendName ASC";
        System.out.println("comrades");
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            stmt.setString(2, "accepted");
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Comrade c = new Comrade();
                c.setCharacterName(rs.getString("characterName"));
                c.setFriendName(rs.getString("friendName").trim());
                cList.add(c);
                System.out.println(c.getFriendName());
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return cList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
    public Vector viewPendingComrades (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Comrade> cList;
            
        cList = new Vector<Comrade>();
        Iterator it = cList.iterator();
                
        getDbcon().connect();
        query = "SELECT characterName, friendName FROM Comrade WHERE characterName = ? AND ctype = ?";
        System.out.println("comrades");
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            stmt.setString(2, "pending");
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Comrade c = new Comrade();
                c.setCharacterName(rs.getString("characterName"));
                c.setFriendName(rs.getString("friendName").trim());
                cList.add(c);
                System.out.println(c.getFriendName());
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return cList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
    public Vector viewRequestComrades (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Comrade> cList;
            
        cList = new Vector<Comrade>();
        Iterator it = cList.iterator();
                
        getDbcon().connect();
        query = "SELECT characterName, friendName FROM Comrade WHERE characterName = ? AND ctype = ?";
        System.out.println("comrades");
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            stmt.setString(2, "request");
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Comrade c = new Comrade();
                c.setCharacterName(rs.getString("characterName"));
                c.setFriendName(rs.getString("friendName").trim());
                cList.add(c);
                System.out.println(c.getFriendName());
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return cList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
     public void deleteComrade (Comrade c){
         		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM Comrade WHERE characterName = ? AND friendName = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, c.getCharacterName());
            stmt.setString(2, c.getFriendName());
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
     }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getFriendName() {
        return friendName;
    }

    public void setFriendName(String friendName) {
        this.friendName = friendName;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }
    public void editType (Comrade c, String value)
   {
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        getDbcon().connect();
        query = "UPDATE Comrade SET ctype = ? WHERE characterName = ? AND friendName = ?";
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, value);
            stmt.setString(2, c.getCharacterName());
            stmt.setString(3, c.getFriendName());
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
   }

    public String getType() {
        return ctype;
    }

    public void setType(String ctype) {
        this.ctype = ctype;
    }
}
